package com.example.team6.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.team6.entity.Sc;
import com.example.team6.entity.Studentwork;
import com.example.team6.entity.Watchstatus;
import com.example.team6.entity.resultt;
import com.example.team6.mapper.ScMapper;
import com.mysql.cj.protocol.WriterWatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.team6.mapper.WatchstatusMapper;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author team6
 * @since 2020-09-10
 */
@RestController
@CrossOrigin
//RequestMapping("/team6/watchstatus")

public class WatchstatusController {
    @Autowired
    WatchstatusMapper watchstatusMapper;

    @Autowired
    ScMapper scMapper;

    @PostMapping(value = "api/addwatch")
    @ResponseBody
    public void watchs (@RequestBody Watchstatus requestUser){


        resultt resultt = new resultt();
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("studentid",requestUser.getStudentid());
        map1.put("courseid",requestUser.getCourseid());
        map1.put("chapterid",requestUser.getChapterid());

        if(watchstatusMapper.selectByMap(map1).size()==0) {
            Integer result = watchstatusMapper.insert(requestUser);
        }
        QueryWrapper<Watchstatus> watchstatusQueryWrapper = new QueryWrapper<>();
        watchstatusQueryWrapper.eq("studentid", requestUser.getStudentid());
        watchstatusQueryWrapper.eq("courseid", requestUser.getCourseid());

        Integer counts = watchstatusMapper.selectCount(watchstatusQueryWrapper);

        System.out.println(counts);

        QueryWrapper<Sc> scQueryWrapper = new QueryWrapper<>();
        scQueryWrapper.eq("studentid", requestUser.getStudentid());
        scQueryWrapper.eq("courseid", requestUser.getCourseid());

        Sc sctemp = new Sc();
        sctemp = scMapper.selectOne(scQueryWrapper);
        sctemp.setVideocount(counts);

        scMapper.updateById(sctemp);
    }


}

